﻿using System;
using System.Collections.Generic;
using KKSG;
using UnityEngine;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000FCF RID: 4047
	internal class PartLoadTask : EquipLoadTask
	{
		// Token: 0x0600D233 RID: 53811 RVA: 0x003103F4 File Offset: 0x0030E5F4
		public PartLoadTask(EPartType p, PartLoadCallback partLoadCb) : base(p)
		{
			this.loadCb = new LoadCallBack(this.LoadFinish);
			this.m_PartLoadCb = partLoadCb;
		}

		// Token: 0x0600D234 RID: 53812 RVA: 0x00310478 File Offset: 0x0030E678
		public override void Load(XEntity e, int prefessionID, ref FashionPositionInfo newFpi, bool async, HashSet<string> loadedPath)
		{
			if (base.IsSamePart(ref newFpi))
			{
				if (this.m_PartLoadCb != null)
				{
					this.m_PartLoadCb(this, false);
				}
			}
			else
			{
				this.Reset(e);
				if (base.MakePath(ref newFpi, loadedPath))
				{
					this.replaceMeshLocation = null;
					this.replaceTexLocation = null;
                    if (XEquipDocument._MeshPartList.GetMeshInfo(this.location, prefessionID, (int)this.part, newFpi.fashionDir, out this.partType, ref this.replaceMeshLocation, ref this.replaceTexLocation))
                    {
                        if (async)
                        {
                            if (this.replaceMeshLocation != null)
                            {
                                this.meshLoadTask = XSingleton<XResourceLoaderMgr>.singleton.GetShareResourceAsync<Mesh>(this.replaceMeshLocation, ".asset", this.loadCb, PartLoadTask.fakeMeshPart);
                            }
                            else
                            {
                                this.meshLoadTask = XSingleton<XResourceLoaderMgr>.singleton.GetShareResourceAsync<Mesh>(this.location, ".asset", this.loadCb, PartLoadTask.fakeMeshPart);
                            }
                            if (this.replaceTexLocation != null)
                            {
                                this.texLoadTask = XSingleton<XResourceLoaderMgr>.singleton.GetShareResourceAsync<Texture2D>(this.replaceTexLocation, ".tga", this.loadCb, PartLoadTask.fakeTexPart);
                                if (this.partType == XMeshPartList.CutoutPart)
                                {
                                    this.alphaLoadTask = XSingleton<XResourceLoaderMgr>.singleton.GetShareResourceAsync<Texture2D>(this.replaceTexLocation + "_A", ".png", this.loadCb, PartLoadTask.fakeAlphaTexPart);
                                }
                            }
                            else
                            {
                                this.texLoadTask = XSingleton<XResourceLoaderMgr>.singleton.GetShareResourceAsync<Texture2D>(this.location, ".tga", this.loadCb, PartLoadTask.fakeTexPart);
                                if (this.partType == XMeshPartList.CutoutPart)
                                {
                                    this.alphaLoadTask = XSingleton<XResourceLoaderMgr>.singleton.GetShareResourceAsync<Texture2D>(this.location + "_A", ".png", this.loadCb, PartLoadTask.fakeAlphaTexPart);
                                }
                            }
                        }
                        else
                        {
                            Mesh sharedResource;
                            if (this.replaceMeshLocation != null)
                            {
                                sharedResource = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<Mesh>(this.replaceMeshLocation, ".asset", true, false);
                            }
                            else
                            {
                                sharedResource = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<Mesh>(this.location, ".asset", true, false);
                            }
                            this.LoadFinish(sharedResource, PartLoadTask.fakeMeshPart);
                            if (this.replaceTexLocation != null)
                            {
                                Texture2D sharedResource2 = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<Texture2D>(this.replaceTexLocation, ".tga", true, false);
                                this.LoadFinish(sharedResource2, PartLoadTask.fakeTexPart);
                                if (this.partType == XMeshPartList.CutoutPart)
                                {
                                    Texture2D sharedResource3 = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<Texture2D>(this.replaceTexLocation + "_A", ".png", true, false);
                                    this.LoadFinish(sharedResource3, PartLoadTask.fakeAlphaTexPart);
                                }
                            }
                            else
                            {
                                Texture2D sharedResource4 = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<Texture2D>(this.location, ".tga", true, false);
                                this.LoadFinish(sharedResource4, PartLoadTask.fakeTexPart);
                                if (this.partType == XMeshPartList.CutoutPart)
                                {
                                    Texture2D sharedResource5 = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<Texture2D>(this.location + "_A", ".png", true, false);
                                    this.LoadFinish(sharedResource5, PartLoadTask.fakeAlphaTexPart);
                                }
                            }
                        }
                    }
                    else
                    {
						this.processStatus = EProcessStatus.EPreProcess;
						if (this.m_PartLoadCb != null)
						{
							this.m_PartLoadCb(this, true);
						}
					}
				}
				else
				{
					if (this.m_PartLoadCb != null)
					{
						this.m_PartLoadCb(this, true);
					}
				}
			}
		}

		// Token: 0x0600D235 RID: 53813 RVA: 0x00310808 File Offset: 0x0030EA08
		private void LoadFinish(UnityEngine.Object obj, object cbOjb)
		{
			if (this.processStatus == EProcessStatus.EProcessing)
			{
				if (cbOjb is PartLoadTask.MeshPart)
				{
					this.mesh = (obj as Mesh);
					this.loadState |= 1;
					this.meshLoadTask = null;
				}
				else
				{
					if (cbOjb is PartLoadTask.TexPart)
					{
						Texture2D texture2D = obj as Texture2D;
						this.tex = texture2D;
						this.texLoadTask = null;
						this.loadState |= 2;
					}
					else
					{
						if (cbOjb is PartLoadTask.AlphaTexPart)
						{
							Texture2D texture2D2 = obj as Texture2D;
							this.aplhaTex = texture2D2;
							this.alphaLoadTask = null;
							this.loadState |= 4;
						}
						else
						{
							XSingleton<XDebug>.singleton.AddErrorLog("assert error equip type", null, null, null, null, null);
						}
					}
				}
				if (XMeshPartList.ConvertType(this.partType) == this.loadState)
				{
					this.processStatus = EProcessStatus.EPreProcess;
					if (this.m_PartLoadCb != null)
					{
						this.m_PartLoadCb(this, true);
					}
				}
			}
		}

		// Token: 0x0600D236 RID: 53814 RVA: 0x00310924 File Offset: 0x0030EB24
		public override void Reset(XEntity e)
		{
			if (this.mesh != null)
			{
				if (!string.IsNullOrEmpty(this.replaceMeshLocation))
				{
					XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(this.replaceMeshLocation, ".asset", this.mesh, XSingleton<XScene>.singleton.SceneType == SceneType.SCENE_HALL);
				}
				else
				{
					XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(this.location, ".asset", this.mesh, XSingleton<XScene>.singleton.SceneType == SceneType.SCENE_HALL);
				}
				this.mesh = null;
			}
			this.replaceMeshLocation = null;
			if (this.tex != null)
			{
				if (!string.IsNullOrEmpty(this.replaceTexLocation))
				{
					XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(this.replaceTexLocation, ".tga", this.tex, XSingleton<XScene>.singleton.SceneType == SceneType.SCENE_HALL);
				}
				else
				{
					XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(this.location, ".tga", this.tex, XSingleton<XScene>.singleton.SceneType == SceneType.SCENE_HALL);
				}
				this.tex = null;
			}
			if (this.aplhaTex != null)
			{
				XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(this.location + "_A", ".png", this.aplhaTex, XSingleton<XScene>.singleton.SceneType == SceneType.SCENE_HALL);
				this.aplhaTex = null;
			}
			this.replaceTexLocation = null;
			this.loadState = 0;
			if (this.meshLoadTask != null)
			{
				this.meshLoadTask.CancelLoad(this.loadCb);
				this.meshLoadTask = null;
            }
            if (this.texLoadTask != null)
			{
				this.texLoadTask.CancelLoad(this.loadCb);
				this.texLoadTask = null;
			}
			if (this.alphaLoadTask != null)
			{
				this.alphaLoadTask.CancelLoad(this.loadCb);
				this.alphaLoadTask = null;
			}
			base.Reset(e);
		}

		// Token: 0x0600D237 RID: 53815 RVA: 0x00310B24 File Offset: 0x0030ED24
		public bool HasMesh()
		{
			return this.mesh != null;
		}

		// Token: 0x0600D238 RID: 53816 RVA: 0x00310B44 File Offset: 0x0030ED44
		public Texture2D GetTexture()
		{
			return this.tex;
		}

		// Token: 0x0600D239 RID: 53817 RVA: 0x00310B5C File Offset: 0x0030ED5C
		public Texture2D GetAlpha()
		{
			return this.aplhaTex;
		}

		// Token: 0x04005F71 RID: 24433
		private string replaceMeshLocation = null;

		// Token: 0x04005F72 RID: 24434
		private string replaceTexLocation = null;

		// Token: 0x04005F73 RID: 24435
		public byte partType = 0;

		// Token: 0x04005F74 RID: 24436
		public Mesh mesh = null;

		// Token: 0x04005F75 RID: 24437
		private Texture2D tex = null;

		// Token: 0x04005F76 RID: 24438
		private Texture2D aplhaTex = null;

		// Token: 0x04005F77 RID: 24439
		private byte loadState = 0;

		// Token: 0x04005F78 RID: 24440
		private static PartLoadTask.MeshPart fakeMeshPart = new PartLoadTask.MeshPart();

		// Token: 0x04005F79 RID: 24441
		private static PartLoadTask.TexPart fakeTexPart = new PartLoadTask.TexPart();

		// Token: 0x04005F7A RID: 24442
		private static PartLoadTask.AlphaTexPart fakeAlphaTexPart = new PartLoadTask.AlphaTexPart();

		// Token: 0x04005F7B RID: 24443
		private LoadAsyncTask meshLoadTask = null;

		// Token: 0x04005F7C RID: 24444
		private LoadAsyncTask texLoadTask = null;

		// Token: 0x04005F7D RID: 24445
		private LoadAsyncTask alphaLoadTask = null;

		// Token: 0x04005F7E RID: 24446
		private LoadCallBack loadCb = null;

		// Token: 0x04005F7F RID: 24447
		private PartLoadCallback m_PartLoadCb = null;

		// Token: 0x020019F8 RID: 6648
		private class MeshPart
		{
		}

		// Token: 0x020019F9 RID: 6649
		private class TexPart
		{
		}

		// Token: 0x020019FA RID: 6650
		private class AlphaTexPart
		{
		}
	}
}
